package com.chookss.home.other;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.chookss.R;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.view.View.SCROLLBARS_OUTSIDE_OVERLAY;

public class StaticWebActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.webView)
    WebView webView;
    private String title, url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static_web);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        commonTitleTxt.setText(title);
        initWebViewSettings();
        showLoading();
        webView.loadUrl(url);
    }
    private void initWebViewSettings() {
        WebSettings webSetting = webView.getSettings();
        webSetting.setDefaultTextEncodingName("utf-8");
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowContentAccess(true); // 是否可访问Content Provider的资源，默认值 true
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
//        webView.addJavascriptInterface(new JavaScriptInterface(), "android");
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            webSetting.setAllowFileAccessFromFileURLs(false);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            webSetting.setMixedContentMode(0);
        }
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                try {
                    dismissLoading();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
//    class JavaScriptInterface {
//        @JavascriptInterface
//        public void title(String title) {
//        }
//
//        @JavascriptInterface
//        public void closeLoading() {
//            try {
//                dismissLoading();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
    @OnClick({R.id.common_title_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
        }
    }
}
